package com.useeinfo.oa.modules.base.thread;

import com.useeinfo.framework.sugar.data.QueryParam;
import com.useeinfo.framework.sugar.tools.Configuration;
import com.useeinfo.framework.sugar.tools.DateTimeUtils;
import com.useeinfo.oa.modules.base.biz.CustomerRepairBiz;
import com.useeinfo.oa.modules.base.entity.CustomerRepair;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.*;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service("customerRepairSecheduled")
public class CustomerRepairSecheduled {

    private final static Logger logger = LoggerFactory.getLogger(CustomerRepairSecheduled.class);

    @Autowired
    private CustomerRepairBiz customerRepairBiz;

    public void execute() {
        logger.info("同步400报修");

        Date now = new Date();

        Calendar beforeTime = Calendar.getInstance();
        beforeTime.add(Calendar.MINUTE, -5);// 5分钟之前的时间
        Date beforeD = beforeTime.getTime();
        String before5 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(beforeD);  // 前五分钟时间

        Connection conn = null;
        try {
            String sql;
            String url = Configuration.getConfigurationByName("jdbcUrl");
            String user = Configuration.getConfigurationByName("username");
            String password = Configuration.getConfigurationByName("password");
            Class.forName("com.mysql.jdbc.Driver");

            conn = DriverManager.getConnection(url, user, password);
            // Statement里面带有很多方法，比如executeUpdate可以实现插入，更新和删除等
            Statement stmt = conn.createStatement();
            sql = "select calldate,src,dst,userfield from cdro_" + DateTimeUtils.format(now,"yyyy_M_d") + " where disposition = 'ANSWERED' and calldate >= '" + before5 +"' ";
            ResultSet rs = stmt.executeQuery(sql);// executeQuery会返回结果的集合，否则返回空值
            while (rs.next()) {
                QueryParam queryParam = new QueryParam();
                queryParam.getSqlMap().put("callDate",DateTimeUtils.format(DateTimeUtils.parse("yyyy-MM-dd HH:mm:ss",rs.getString(1)),"yyyy-MM-dd HH:mm:ss"));
                List<CustomerRepair> list = customerRepairBiz.findList(queryParam);
                if(CollectionUtils.isEmpty(list)){
                    CustomerRepair customerRepair = new CustomerRepair();
                    customerRepair.setCallDate(DateTimeUtils.parse("yyyy-MM-dd HH:mm:ss",rs.getString(1)));
                    customerRepair.setSrc(rs.getString(2));
                    customerRepair.setDst(rs.getString(3));
                    customerRepair.setRecordPath(rs.getString(4));
                    customerRepairBiz.addOrUpdate(customerRepair);
                }

            }
        } catch (Exception e) {
            logger.info("同步失败",e);
            e.printStackTrace();
        } finally {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        logger.info("同步成功");
    }
}
